<?php

namespace common\repositories\outstock;

use common\entities\outstock\MyscrmDeliverySubOrderEntity;
use common\entities\outstock\MyscrmDeliveryOrderEntity;
use common\repositories\BaseRepository;
use common\repositories\outstock\DeliveryOrderRepository;
use common\support\StringHelper;

/**
 * Created by PhpStorm.
 * User: yuyj
 * Date: 16-12-12
 * Time: 上午11:00
 */
class DeliverySubOrderRepository extends BaseRepository
{
    public static $strToSignTypeMap=['新签'=>1,'续签'=>3,'增购'=>2];
    public function addContract($data){
        if(!isset($data['sub_order_id'])||empty($data['sub_order_id'])){
            $data['sub_order_id']=StringHelper::uuid();
        }
        $entity=new MyscrmDeliverySubOrderEntity();
        $entity->load($data,'');
        $saveRes=$entity->save();
        if($saveRes===false){
            return self::setAndReturn(__LINE__,$entity->getAllFirstErrorMessage());
        }
        return $saveRes;
    }
    public function getTodoList($page,$pageSize,$type,$status,$keyword,$timeQuantum){
        $startTime=$timeQuantum['startTime'];
        $endTime=$timeQuantum['endTime'];
        $flowStatus=[DeliveryOrderRepository::STATUS_YQR,DeliveryOrderRepository::STATUS_YKT];
        if($status){
            switch ($status){
                case 30:
                    $flowStatus=[DeliveryOrderRepository::STATUS_YQR];
                    break;
                case 40:
                    $flowStatus=[DeliveryOrderRepository::STATUS_YKT];
            }
        }
        $query=MyscrmDeliverySubOrderEntity::find()
            ->from(MyscrmDeliverySubOrderEntity::tableName().' t1')
            ->select('t1.sub_order_id,t1.created_on as confirm_time,t1.flow_status,t1.sub_order_index,t1.sub_order_code,t1.erp_contract_id,t1.erp_contract_code,t2.*')
            ->leftJoin(MyscrmDeliveryOrderEntity::tableName(). ' t2', 't1.delivery_guid = t2.delivery_guid')
            ->where(['flow_status'=>$flowStatus]);
        if($type){
            $query=$query->andWhere(['t2.delivery_scene'=>$type]);
        }
        if($startTime&&$endTime){
            $query=$query->andWhere(['between','t2.created_on',$startTime.' 00:00:00',$endTime.' 23:59:59']);
        }else if($startTime){
            $query=$query->andWhere(['>=','t2.created_on',$startTime.' 00:00:00']);
        }else if($endTime){
            $query=$query->andWhere(['<','t2.created_on',$endTime.' 23:59:59']);
        }
        if($keyword){
            $query=$query->andWhere(['OR',['LIKE','t2.deliver_name',$keyword],['LIKE','t2.customer_full_name',$keyword]]);
        }
        $list = $query
            ->limit($pageSize)
            ->offset(($page-1) * $pageSize)
            ->orderBy('t2.created_on desc')
            ->asArray()
            ->all();
        $count = $query->count(1);
        return ['data' => $list, 'total' => $count];
    }
    public function getDoneList($page,$pageSize,$type,$keyword,$timeQuantum){
        $startTime=$timeQuantum['startTime'];
        $endTime=$timeQuantum['endTime'];
        $query=MyscrmDeliverySubOrderEntity::find()
            ->from(MyscrmDeliverySubOrderEntity::tableName().' t1')
            ->select('t1.sub_order_id,t1.created_on as confirm_time,t1.flow_status,t1.sub_order_index,t1.sub_order_code,t1.erp_contract_id,t1.erp_contract_code,t2.*')
            ->leftJoin(MyscrmDeliveryOrderEntity::tableName(). ' t2', 't1.delivery_guid = t2.delivery_guid')
            ->where(['flow_status'=>DeliveryOrderRepository::STATUS_YSQ]);
        if($type){
            $query=$query->andWhere(['t2.delivery_scene'=>$type]);
        }
        if($startTime&&$endTime){
            $query=$query->andWhere(['between','t2.created_on',$startTime.' 00:00:00',$endTime.' 23:59:59']);
        }else if($startTime){
            $query=$query->andWhere(['>=','t2.created_on',$startTime.' 00:00:00']);
        }else if($endTime){
            $query=$query->andWhere(['<','t2.created_on',$endTime.' 23:59:59']);
        }
        if($keyword){
            $query=$query->andWhere(['OR',['LIKE','t2.deliver_name',$keyword],['LIKE','t2.customer_full_name',$keyword]]);
        }
        $list = $query
            ->limit($pageSize)
            ->offset(($page-1) * $pageSize)
            ->orderBy('t2.created_on desc')
            ->asArray()
            ->all();
        $count = $query->count(1);
        return ['data' => $list, 'total' => $count];
    }
    public function findDetailBySubOrderId($subOrderId){
        return MyscrmDeliverySubOrderEntity::find()
            ->from(MyscrmDeliverySubOrderEntity::tableName() . ' t1')
            ->select('t2.*,t1.sub_order_id,t1.created_on as confirm_time,t1.flow_status,t1.sub_order_index,t1.sub_order_code,t1.erp_contract_id,t1.erp_contract_code')
            ->leftJoin(MyscrmDeliveryOrderEntity::tableName() . ' t2', 't1.delivery_guid = t2.delivery_guid')
            ->where(['t1.sub_order_id' => $subOrderId])
            ->asArray(true)
            ->one();
    }
    /**
     * @return MyscrmDeliverySubOrderEntity
     */
    public function findBySubOrderId($subOrderId){
        return MyscrmDeliverySubOrderEntity::findOne(['sub_order_id' => $subOrderId]);
    }
    public function findDetailBySubOrderCode($subOrderCode){
        return MyscrmDeliverySubOrderEntity::find()
            ->from(MyscrmDeliverySubOrderEntity::tableName() . ' t1')
            ->select('t1.sub_order_id,t1.created_on as confirm_time,t1.flow_status,t1.sub_order_index,t1.sub_order_code,t1.erp_contract_id,t1.erp_contract_code,t2.*')
            ->leftJoin(MyscrmDeliveryOrderEntity::tableName() . ' t2', 't1.delivery_guid = t2.delivery_guid')
            ->where(['t1.sub_order_code' => $subOrderCode])
            ->asArray(true)
            ->one();
    }
    public function findBySubOrderCode($subOrderCode){
        return MyscrmDeliverySubOrderEntity::findOne(['sub_order_code' => $subOrderCode]);
    }
    public function getNotDoneCount($deliveryGuid){
       return MyscrmDeliverySubOrderEntity::find()
            ->where(['<>','flow_status',DeliveryOrderRepository::STATUS_YSQ])
            ->andFilterWhere(['delivery_guid'=>$deliveryGuid])
            ->count(1);
    }
    public function updateSubOrdersFlowStatusByOrderId($deliveryGuid,$status){
        $sql="update ".MyscrmDeliverySubOrderEntity::tableName()." set flow_status=:flow_status  where delivery_guid=:delivery_guid";
        $params=[
            ':delivery_guid'=>$deliveryGuid,
            ':flow_status'=>$status
        ];
        return MyscrmDeliverySubOrderEntity::getDb()->createCommand($sql,$params)->execute();
    }
    public function updateSubOrderFlowStatusById($subOrderId,$status){
        $sql="update ".MyscrmDeliverySubOrderEntity::tableName()." set flow_status=:flow_status  where sub_order_id=:sub_order_id";
        $params=[
            ':sub_order_id'=>$subOrderId,
            ':flow_status'=>$status
        ];
        return MyscrmDeliverySubOrderEntity::getDb()->createCommand($sql,$params)->execute();
    }
}